package com.ytjj.qmyx.admin.service.impl;

import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.UserDao;
import com.ytjj.qmyx.admin.dao.UsersBalanceRecordDao;
import com.ytjj.qmyx.admin.model.request.UsersBalanceRecordRequest;
import com.ytjj.qmyx.admin.service.UsersBalanceRecordService;
import com.ytjj.qmyx.users.mapper.UsersInfoMapper;
import com.ytjj.qmyx.users.model.UsersInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UsersBalanceRecordServiceImpl implements UsersBalanceRecordService {

    @Autowired
    private UsersBalanceRecordDao usersBalanceRecordDao;

    @Autowired
    private UsersInfoMapper usersInfoMapper;

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRecord(UsersBalanceRecordRequest usersBalanceRecordRequest) {
        UsersInfo usersInfo = usersInfoMapper.selectByPrimaryKey(usersBalanceRecordRequest.getUserId());
        //用户增加余额前的金额、总积累金额、积累佣金
        BigDecimal beforeMoney = usersInfo.getMoney();
        BigDecimal beforeTotalMoney = usersInfo.getTotalMoney();
        BigDecimal beforeBrokerageNum = usersInfo.getBrokerageNum();
        //用户增加余额后的金额、总积累金额、积累佣金
        BigDecimal addAfterMoney = beforeMoney.add(usersBalanceRecordRequest.getBalance());
        BigDecimal addAfterTotalMoney = beforeTotalMoney.add(usersBalanceRecordRequest.getBalance());
        BigDecimal addAfterBrokerageNum = beforeBrokerageNum.add(usersBalanceRecordRequest.getBalance());
        //用户减少余额后的金额、积累佣金
        BigDecimal subAfterMoney = beforeMoney.subtract(usersBalanceRecordRequest.getBalance()).setScale(2, BigDecimal.ROUND_UP);
        BigDecimal subAfterBrokerageNum = beforeBrokerageNum.subtract(usersBalanceRecordRequest.getBalance()).setScale(2, BigDecimal.ROUND_UP);
        int count = 0;
        usersBalanceRecordRequest.setOriginalBalance(beforeMoney);
        usersBalanceRecordRequest.setCreateTime(new Date());
        if ("ADD".equals(usersBalanceRecordRequest.getType())) {
            usersBalanceRecordRequest.setNewBalance(addAfterMoney);
            count = usersBalanceRecordDao.insertRecord(usersBalanceRecordRequest);
            if (count > 0) {
                usersBalanceRecordRequest.setNewMoney(addAfterMoney);
                usersBalanceRecordRequest.setNewTotalMoney(addAfterTotalMoney);
                usersBalanceRecordRequest.setNewBrokerageNum(addAfterBrokerageNum);
                userDao.updateMoney(usersBalanceRecordRequest);
            }
        } else if("SUB".equals(usersBalanceRecordRequest.getType())) {
            usersBalanceRecordRequest.setNewBalance(subAfterMoney);
            count = usersBalanceRecordDao.insertRecord(usersBalanceRecordRequest);
            if (count > 0) {
                usersBalanceRecordRequest.setNewMoney(subAfterMoney);
                usersBalanceRecordRequest.setNewTotalMoney(beforeTotalMoney);
                usersBalanceRecordRequest.setNewBrokerageNum(subAfterBrokerageNum);
                userDao.updateMoney(usersBalanceRecordRequest);
            }
        } else {
            throw new ApiException("分配类型(增加/减少)不能为空!");
        }

        return count;
    }
}
